package org.hugoduncan.appam.config.impl;

import java.io.IOException;
import java.io.InputStream;

import org.apache.abdera.model.Document;
import org.apache.abdera.model.Service;
import org.apache.abdera.model.Element;
import org.apache.abdera.parser.Parser;
import org.apache.abdera.parser.ParserOptions;

public class AtomResourceLoader
{
    public static <T extends Element> T loadElement(String path, Class<T> clazz)
        throws IOException
    {
        // find the jdo.properties in the class path
        InputStream inStream = Service.class.getResourceAsStream(path);
        if (inStream == null)
            throw new IOException("Resource not found: " + path);

        // remove comments from loaded resources
        ParserOptions parserOptions=Parser.INSTANCE.getDefaultParserOptions();
        //parserOptions.setIgnoreComments(true);
        parserOptions.setIgnoreWhitespace(true);
        Parser.INSTANCE.setDefaultParserOptions(parserOptions);
        Document<T> element=Parser.INSTANCE.parse(inStream);
        return element.getRoot();
    }
}
